﻿using System;

using Wicresoft.Framework.Organization;
using MaLu.BusiLogic;
using MaLu.Common;
using System.Data;
using MaLu.Data;
using MaLu.Framework.Common;
using System.Xml;

namespace MaluJg.web.Visite
{
    public partial class AddVisite :VisitePage
    {
        public Guid DepartmentID
        {
            get
            {
                if (Request["DepartmentID"] != null)
                    return new Guid(Request["DepartmentID"]);
                return Guid.NewGuid();
            }
        }



        protected void Page_Load(object sender, EventArgs e)
        {
            tbStart.Attributes.Add("readonly", "readonly");
            tbEnd.Attributes.Add("readonly", "readonly");

            if (!IsPostBack)
            {
                if (VisiteID != Guid.Empty)
                {

                    DataTable dt = SqlHelper.Instance.ExecuteDataTable(Provider, "select * from Visite where id ='" + VisiteID.ToString() + "'");
                    if (dt.Rows.Count > 0)
                    {
                        tbName.Text = dt.Rows[0]["Leader"].ToString();
                        tbRemark.Text = dt.Rows[0]["Remark"].ToString();
                        tbContact.Text = dt.Rows[0]["Contact"].ToString();
                        tbVisitee.Text = dt.Rows[0]["Interviewee"].ToString();
                        if (dt.Rows[0]["ArrangeStartTime"].ToString() !=string.Empty)
                        {
                            tbStart.Text = DateTime.Parse(dt.Rows[0]["ArrangeStartTime"].ToString()).ToString("yyyy-MM-dd");
                            
                        }
                        if (dt.Rows[0]["ArrangeEndTime"].ToString() != string.Empty)
                        {
                            tbEnd.Text = DateTime.Parse(dt.Rows[0]["ArrangeEndTime"].ToString()).ToString("yyyy-MM-dd");
                            
                        }
                    }
                }
            }
        }
        protected void btnSend_Click(object sender, EventArgs e)
        {
            Department d = Wicresoft.Framework.Organization.OrganizationHelper.GetDepartmentInfo(DepartmentID);

            BLVisite.SaveBLVisite(Provider, VisiteID, tbName.Text, tbVisitee.Text, DateTime.Parse(tbStart.Text + " 0:0:0"), DateTime.Parse(tbEnd.Text + " 0:0:0"),
                tbContact.Text,tbRemark.Text,CurrentUser.Name,CurrentUser.MainDepartmentID,d.Name,d.DisplayName);
            BLVisite.SaveVisiteItem(Provider, VisiteID, "item1.aspx", FillContent());
            ClientScript.RegisterStartupScript(this.GetType(), "Close", "<script>alert('成功保存!');window.close();</script>");
        }
        private string FillContent()
        {
            string template = BLVisite.GetTemplate(Provider, "item1.aspx");
            if (template != string.Empty)
            {

                XmlDocument document = new XmlDocument();
                document.LoadXml(template);
                XmlElement xe = document.SelectSingleNode("//领导姓名") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbName.Text.Trim());
                xe = document.SelectSingleNode("//走访单位") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbVisitee.Text.Trim());
                xe = document.SelectSingleNode("//时间安排") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbStart.Text.Trim() + "|" + tbEnd.Text.Trim());
                xe = document.SelectSingleNode("//联系人") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbContact.Text.Trim());
                xe = document.SelectSingleNode("//内容和要求") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbRemark.Text.Trim());



                return document.OuterXml;
            }
            else
                return string.Empty;
        }
    }
}
